﻿using GodSystemBLL;
using GodSystemCommon;
using GodSystemModel;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GodSystem.FormChildren
{
    public partial class FormAddPower : Form
    {
        public FormAddPower()
        {
            InitializeComponent();
            InitData();
        }

        private void InitData()
        {
            PowersBLL powersBLL = new PowersBLL();
            List<PowerGroupModel> powerGroup = powersBLL.GetPowerGroup();
            ArrayList textValue = new ArrayList();

            for(int i = 0; i < powerGroup.Count; i++)
            {
                textValue.Add(new TextAndValue(powerGroup[i].GroupName, powerGroup[i].Id.ToString()));
            }
            comboBox_GroupName.DataSource = textValue;
            comboBox_GroupName.DisplayMember = "DisplayText";
            comboBox_GroupName.ValueMember = "RealValue";
           

        }

        private void button_PowerCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void button_PowerAdd_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(comboBox_GroupName.SelectedValue);
            string node = textBox_PowerNode.Text.Trim();
            string remark = textBox_PowerRemark.Text.Trim();
            if(string.IsNullOrEmpty(node) || string.IsNullOrEmpty(remark))
            {
                MessageBox.Show("节点和名称不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (id == 0)
            {
                MessageBox.Show("请选择对应的分组", "提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                return;
            }

            PowersBLL powersBLL = new PowersBLL();
            int num =Convert.ToInt32(powersBLL.GetPower(node, remark));
            if(num > 0)
            {
                MessageBox.Show("对应的权限已经存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            int result = powersBLL.AddPower(node, remark, id);
            if (result > 0)
            {
                MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                textBox_PowerNode.Text = "";
                textBox_PowerRemark.Text = "";
            }
            else
            {
                MessageBox.Show("添加失败，请稍后再试", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
    }
}
